import { useGlobalStore } from "../store/global";
import { useEventBus } from "@vueuse/core";

export default defineNuxtPlugin((nuxtApp) => {
  const globalStore = useGlobalStore();
  const useLanguage = useEventBus("language-change");

  // called right before setting a new locale
  nuxtApp.hook(
    "i18n:beforeLocaleSwitch",
    ({ oldLocale, newLocale, initialSetup, context }) => {
      console.log("语言即将切换：", oldLocale, newLocale, initialSetup);
    }
  );

  // called right after a new locale has been set （由于skipSettingLocaleOnNavigate这个属性，导致语言更改失效）
  nuxtApp.hook("i18n:localeSwitched", ({ oldLocale, newLocale }) => {
    console.log(nuxtApp, "语言已经切换", oldLocale, newLocale);
    // 目前 全局只有 03.callApp.client 总才可以切换语言
    if (globalStore?.$state.language !== newLocale) {
      useLanguage.emit(globalStore?.$state.language);
    }
  });
});
